/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright (c) 2008-2011, Salzburg Research. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * - Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * - Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 * - Neither the name of the KiWi Project nor the names of its contributors
 * may be used to endorse or promote products derived from this software
 * without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 * Contributor(s):
 * Thomas Kurz
 *
 * File Info:
 * User: Thomas Kurz
 * Date: 2011/02/07
 * Time: 12:37
 *
 * Project: KiWi2, kiwi-core module
 */

package kiwi.core.services.content;

import kiwi.core.api.config.ConfigurationService;
import kiwi.core.api.content.ContentService;
import kiwi.core.api.facading.FacadingService;
import kiwi.core.api.triplestore.LiteralService;
import kiwi.core.exception.KiWiException;
import kiwi.core.model.content.MediaContent;
import kiwi.core.model.content.MediaContentItem;
import kiwi.core.model.rdf.KiWiMediaContentLiteral;
import kiwi.core.model.rdf.KiWiResource;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;

/**
 * User: Thomas Kurz
 * Date: 07.02.11
 * Time: 12:37
 */
@ApplicationScoped
public class ContentServiceImpl implements ContentService {


	@Inject
    private FacadingService facadingService;

	@Inject
	private LiteralService literalService;

	@Inject
	private ConfigurationService configurationService;

	@Override
	public void setContent(KiWiResource resource, byte[] data, String mimetype) {
		MediaContent media = new MediaContent(data,mimetype);
		//create facade
		MediaContentItem mci = facadingService.createFacade(resource, MediaContentItem.class);
		//create and add literal
		KiWiMediaContentLiteral literal = literalService.createMediaContentLiteral(media);
		mci.setMediaContent(literal);
	}

	@Override
	public byte[] getContent(KiWiResource resource, String mimetype) {
		MediaContentItem mci = facadingService.createFacade(resource, MediaContentItem.class);
		if(mci.getMediaContent() != null) return mci.getMediaContent().getData();
		return null;
	}

	@Override
	public boolean hasContent(KiWiResource resource, String mimetype) {
		MediaContentItem mci = facadingService.createFacade(resource, MediaContentItem.class);
		if(mci.getMediaContent() != null) return mci.getMediaContent().getMimeType().equals(mimetype);
		return false;
	}

	@Override
	public String getContentType(KiWiResource resource) {
		MediaContentItem mci = facadingService.createFacade(resource, MediaContentItem.class);
		if(mci.getMediaContent() != null) return mci.getMediaContent().getMimeType();
		return null;
	}

	@Override
	public boolean deleteContent(KiWiResource resource) throws KiWiException {
		MediaContentItem mci = facadingService.createFacade(resource, MediaContentItem.class);
		if(mci.getMediaContent() == null) return false;
		mci.setMediaContent(null);
		return true;
	}
}
